route.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { fetchJson } from '@/lib/utils/server';
  4. export async function GET(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  5. const { path } = await params;
  6. const endpoint = `/api/forum/comments/${(path ?? []).join('/')}`;
  7. const url = new URL(request.url);
  8. const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, {
  9. method: 'GET'
  10. });
  11. return NextResponse.json(res);
  12. }
  13. export async function POST(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  14. const { path } = await params;
  15. const endpoint = `/api/forum/comments/${(path ?? []).join('/')}`;
  16. const contentType = request.headers.get('content-type') || '';
  17. if (contentType.includes('multipart/form-data')) {
  18. const res: ResultDto = await fetchJson(endpoint, {
  19. method: 'POST',
  20. body: await request.arrayBuffer(),
  21. headers: { 'Content-Type': contentType }
  22. });
  23. return NextResponse.json(res);
  24. }
  25. const res: ResultDto = await fetchJson(endpoint, {
  26. method: 'POST',
  27. body: JSON.stringify(await request.json())
  28. });
  29. return NextResponse.json(res);
  30. }
  31. export async function PUT(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  32. const { path } = await params;
  33. const endpoint = `/api/forum/comments/${(path ?? []).join('/')}`;
  34. const contentType = request.headers.get('content-type') || '';
  35. if (contentType.includes('multipart/form-data')) {
  36. const res: ResultDto = await fetchJson(endpoint, {
  37. method: 'PUT',
  38. body: await request.arrayBuffer(),
  39. headers: { 'Content-Type': contentType }
  40. });
  41. return NextResponse.json(res);
  42. }
  43. const res: ResultDto = await fetchJson(endpoint, {
  44. method: 'PUT',
  45. body: JSON.stringify(await request.json())
  46. });
  47. return NextResponse.json(res);
  48. }
  49. export async function DELETE(_: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  50. const { path } = await params;
  51. const endpoint = `/api/forum/comments/${(path ?? []).join('/')}`;
  52. const res: ResultDto = await fetchJson(endpoint, {
  53. method: 'DELETE'
  54. });
  55. return NextResponse.json(res);
  56. }